package qdu.guisockets.clientlet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame{
    static String SERVERIP = "127.0.0.1";
    static int SERVERPORT = 9999;
    JPanel p;
	JTextField tfIP, tfPort;
	JButton btn;
    JLabel _serverIP,_serverPort;
    LoginFrame(){
        p = new JPanel(new GridLayout(3,2));
		tfIP = new JTextField(); tfPort = new JTextField();
        _serverIP = new JLabel("服务端IP:");
        _serverPort = new JLabel("服务端Port:");
		btn = new JButton("go");
    }
    public void initUI() {
        p.add(_serverIP); p.add(tfIP); 
        p.add(_serverPort); p.add(tfPort);
        p.add(new JLabel()); p.add(btn);
		this.add(p);
		
		this.setSize(300, 200);
		this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                LoginFrame.this.btn.setEnabled(false);
                String IP = tfIP.getText().trim();
                if(IP != null && !"".equals(IP))//改进: 可用正则表达式检测匹配
                    SERVERIP = IP;
                else
                    SERVERIP = "127.0.0.1";
                try {
                    SERVERPORT = Integer.valueOf(tfPort.getText().trim());
                } catch (Exception ex) {
                    System.out.println("非法端口号");
                    SERVERPORT = 9999;
                }
                LoginFrame.this.setVisible(false);
                LoginFrame.this.dispose();
                TestSocketClient tsc = new TestSocketClient();
                tsc.connetToServer();
            }
        });
    }
}
